package com.tianjin.weight.service;

import com.tianjin.weight.behaviour.base.PermissionBehavior;
import com.tianjin.weight.behaviour.base.RoleBehavior;
import com.tianjin.weight.behaviour.base.RolePermissionBehavior;
import com.tianjin.weight.entity.Ao;
import com.tianjin.weight.entity.SysRole;
import com.tianjin.weight.entity.SysRoleCompound;
import com.tianjin.weight.exception.BusinessException;
import com.tianjin.weight.exception.code.BaseResponseCode;
import com.tianjin.weight.model.common.Role;
import com.tianjin.weight.service.base.RoleService;
import com.tianjin.weight.vo.resp.PermissionRespNode;
import org.springframework.beans.BeanUtils;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class RoleWithBigService extends RoleService implements com.tianjin.weight.behaviour.interfaces.RoleBehavior {

    public RoleWithBigService(Role roleModel, RoleBehavior roleBehavior, PermissionBehavior permissionBehavior, RolePermissionBehavior rolePermissionBehavior, Ao ao) {
        super(roleModel, roleBehavior, permissionBehavior, rolePermissionBehavior, ao);
    }

    @Override
    public SysRoleCompound getOne() throws Exception {
        SysRole one = (SysRole) this.roleModel.getOne();
        if(one==null){
            throw new BusinessException(BaseResponseCode.DATA_ERROR);
        }
        SysRoleCompound sysRoleCompound = SysRoleCompound.builder().build();
        BeanUtils.copyProperties(one,sysRoleCompound);
        List<PermissionRespNode> permissionRespNodes = permissionBehavior.selectBig();
        Set<Long> checkList = new HashSet<>(rolePermissionBehavior.getPermissionIdsByRoleId(one.getId()));
        roleBehavior.setheckced(permissionRespNodes,checkList);
        sysRoleCompound.setPermissionRespNodes(permissionRespNodes);
        return  sysRoleCompound;
    }

}
